<?php
	include("includes/secure.php");
	include("includes/class/afp.class.php");	
	include("includes/config.php");
	include("includes/class/paginador.class.php");
	
	$afp = new Afp(); 	
	$parametrosBuscador = '';
	
	$msg_info = $msg_alert = $msg_error = false;	
	$show_msg = 0;
	
	if( isset( $_GET['show_msg']) )
		$show_msg = $_GET['show_msg'];
	else
		$_SESSION['msg_cool'] = '';
	
	if(isset ($_GET['inicio']))
		$inicio = $_GET['inicio'];
	else
		$inicio = 0;
	
	if(isset ($_GET['cantidad']))
		$cantidad = $_GET['cantidad'];
	else
		$cantidad = 10;
	
	if(isset($_POST['search']) && $_POST['search']!=''){
		$search = $_POST['search'];
		$parametrosBuscador .= '&search='.$search;
		$inicio = 0;
	}
	elseif(isset($_GET['search']) && $_GET['search']!=''){
		$search = $_GET['search'];
		$parametrosBuscador .= '&search='.$search;
	}
	else
		$search='';

   
   	//<ORDER_BY>
	//VARS CONFIG
	$fields_sql_order_by  = array("afp_name","afp_porc","arp_description","");
	$field_is_href = array(1,0);//para saber si es o no campo para ordenar
	$fields_name_order_by = array("NOMBRE AFP","PORCENTAJE","DESCRIPCION","OPCIONES");
	$item_width = array("20%","10%","50%","");
	$method_order = "GET";
	$ord_default = 'DESC';
	$class_href_order = 'lbl_white';//clase del href	
	$index_order_default = 0;//por cual ordenar en primer vez
	$img_order_desc = "images/down_arrow.gif";
	$img_order_asc  = "images/up_arrow.gif";
	//END VARS CONFIG
	if($method_order == 'GET' && isset($_GET['order_by']) && isset($_GET['ord'])){
		$order_by = $_SESSION['order_by'] = $_GET['order_by'];
		$ord = $_SESSION['ord'] = $_GET['ord'];
		$isset_order = true;
	}
	elseif($method_order == 'POST' && isset($_POST['order_by']) && isset($_POST['ord'])){
		$order_by = $_SESSION['order_by'] = $_POST['order_by'];
		$ord = $_SESSION['ord'] = $_POST['ord'];
		$isset_order = true;
	}
	elseif(isset($_SESSION["order_by"]) && in_array($_SESSION["order_by"],$fields_sql_order_by) && trim($_SESSION["order_by"]) != '' && 
			isset($_SESSION["ord"]) && trim($_SESSION["ord"]) != ''){
		$order_by = $_SESSION['order_by'];
		$ord = $_SESSION['ord'];
		$isset_order = true;
	}
	else{
		$order_by = $_SESSION['order_by'] = $fields_sql_order_by[$index_order_default];
		$ord = $_SESSION['ord'] = $ord_default;
		$isset_order = false;	
	}
	
	if($ord == "DESC"){
		$ord_next = 'ASC';
		$title_order = $title_order_asc;
		$img_ord = '<img src="'.$img_order_desc.'" alt="'.$title_order_asc.'" style="border:0px"/>';
	}
	else{
		$ord_next = 'DESC';
		$title_order = $title_order_desc;
		$img_ord = '<img src="'.$img_order_asc.'" alt="'.$title_order_desc.'" style="border:0px"/>';
	}	
		
	$lbl_order = $ord_href = array();
	$parametrosBuscador .= '&order_by='.$order_by.'&ord='.$ord;
	for($i=0;$i<count($fields_sql_order_by);$i++){		
		if($field_is_href[$i]){
			if($isset_order){
				if($order_by == $fields_sql_order_by[$i])
					$img_ord_show = $img_ord;
				else
					$img_ord_show = '';
			}
			elseif($i == $index_order_default)
				$img_ord_show = $img_ord;
			else
				$img_ord_show = '';
			
			if($method_order == 'GET')
				$ord_href[$i] = '<a href="?'.$parametrosBuscador.'&order_by='.$fields_sql_order_by[$i].'&ord='.$ord_next.'" title="'.$title_order.'" class="'.$class_href_order.'">'.$fields_name_order_by[$i].'&nbsp;'.$img_ord_show.'</a>';
			elseif($method_order == 'POST')
				$ord_href[$i] = '<a href="javascript:;" onclick="javascript: orderby_list(&apos;'.$fields_sql_order_by[$i].'&apos;,&apos;'.$ord_next.'&apos;)" title="'.$title_order.'" class="'.$class_href_order.'">'.$fields_name_order_by[$i].'&nbsp;'.$img_ord_show.'</a>';
		}
		else
			$ord_href[$i]='<span class="'.$class_href_order.'">'.$fields_name_order_by[$i].'</span>';
	}
	$orderByList = " ORDER BY ".$order_by.' '.$ord;
	//</ORDER_BY>
	$totalFiles = $afp->countAfp($search);
	$afp = $afp->afpList($search,$orderByList,$inicio, $cantidad);
	
	$paginador = new Paginador($afp,$cantidad,7,$inicio,$totalFiles,0,$parametrosBuscador);
	$siguiente = $paginador->obtenerAtributoPaginador('btnSiguiente');
	$anterior = $paginador->obtenerAtributoPaginador('btnAnterior');
	$paginas = $paginador->obtenerAtributoPaginador('listaPaginas');
	
	$totalPaginas = $paginador->obtenerAtributoPaginador('totalPaginas');
	$paginaActual = $paginador->obtenerAtributoPaginador('paginaActual');
	
	$hasta = $cantidad+$inicio; 
	if($hasta>$totalFiles || $totalFiles==0)
		$hasta=$totalFiles;
	$band_list = 'Mostrando '.($inicio+1).'-'.$hasta.' de '.$totalFiles;	
?>